package com.nolydake.swingnote.gui.ext;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JPanel;

/**
 * 可以绘制图片的面板
 * 
 * @author NOLY DAKE
 *
 */
public class ImagePanel extends JPanel {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 6539904658569948044L;

    public ImagePanel() {
        super();
    }

    /** 是否自动适应窗口大小,false的时候,根据图片的大小进行显示,true的时候,根据窗口的大小进行展示 */
    private boolean adaptive = false;

    public boolean isAdaptive() {
        return adaptive;
    }

    public void setAdaptive(boolean adaptive) {
        this.adaptive = adaptive;
    }

    private Image image = null;

    public void setImage(Image image) {
        this.image = image;
    }

    @Override
    public void paintComponent(Graphics g) {

        if (image != null) {

            Color backg = this.getBackground();
            Color cache = g.getColor();
            g.setColor(backg);

            g.drawRect(0, 0, this.getWidth(), this.getHeight());
            g.setColor(cache);

            if (adaptive) {
                g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
            } else {
                g.drawImage(image, 0, 0, this);
            }

        } else {
            super.paintComponent(g);
        }
    }
}
